package com.focusai.efairy.ui.widget.ninegridview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.focusai.efairy.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * 作者：HMY
 * 时间：2016/5/12
 */
public class NineGridTestLayout extends NineGridLayout {

    protected static final int MAX_W_H_RATIO = 3;

    public NineGridTestLayout(Context context) {
        super(context);
    }

    public NineGridTestLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private RequestOptions requestOptions;
    private OnImageItemClick onImageItemClick;
    @Override
    protected boolean displayOneImage(final RatioImageView imageView, String url, final int parentWidth) {
        Glide.with(mContext).load(url)
                .transition(DrawableTransitionOptions.withCrossFade(300))
                .apply(getOptions()).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {

                int w = resource.getIntrinsicWidth();
                int h = resource.getIntrinsicHeight();

                int newW;
                int newH;
                if (h > w * MAX_W_H_RATIO) {//h:w = 5:3
                    newW = parentWidth / 2;
                    newH = newW * 5 / 3;
                } else if (h < w) {//h:w = 2:3
                    newW = parentWidth * 2 / 3;
                    newH = newW * 2 / 3;
                } else {//newH:h = newW :w
                    newW = parentWidth / 2;
                    newH = h * newW / w;
                }
                setOneImageLayoutParams(imageView, newW, newH);
                imageView.setImageDrawable(resource);
            }
        });
        return false;
    }

    @Override
    protected void displayImage(final RatioImageView imageView, String url) {
        Glide.with(mContext).load(url)
                .transition(DrawableTransitionOptions.withCrossFade(300))
                .apply(getOptions().override(imageView.getWidth(),imageView.getHeight())).into(imageView);
//        ImageLoaderFactory.getImageLoader().loadImage(mContext, imageView, url,R.drawable.image_default_pic,false);
//        Glide.with(mContext).load(url)
//                .transition(DrawableTransitionOptions.withCrossFade(300))
//                .apply(getOptions()).into(new SimpleTarget<Drawable>() {
//            @Override
//            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
//                imageView.setImageDrawable(resource);
//            }
//        });
    }

    @Override
    protected void onClickImage(int i, String url, List<String> urlList) {
        if (onImageItemClick != null){
            onImageItemClick.onImageItemClick(i,url, (ArrayList<String>) urlList);
        }
    }

    public void setOnImageItemClick(OnImageItemClick onImageItemClick) {
        this.onImageItemClick = onImageItemClick;
    }

    private RequestOptions getOptions() {
        if (requestOptions == null) {
            requestOptions = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.image_default_pic)
                    .error(R.drawable.dm_img_forpic_normal)
                    .priority(Priority.HIGH)
                    .dontAnimate()
                    .dontTransform()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }
        return requestOptions;
    }
    public interface OnImageItemClick{
        void onImageItemClick(int position, String url, ArrayList<String> urlList);
    }
}
